/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Control;
import Model.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Administrator
 */
public class FlightsServiceI implements FlightsService {
    private FlightsI ci;
    private FlightsConnection conn = new FlightsConnection();
    private Statement st = null;
    private ResultSet rs = null;

    public FlightsI findByPlaneid(FlightsI ci) {
        this.ci = new FlightsI();
        this.dbConnection();
        this.getResultSet("SELECT * FROM flights WHERE planeid='" + ci.getPlaneid() + "'");
        try {
            this.ci.setPlaneid(rs.getString("planeid"));
            this.ci.setSairport(rs.getString("sairport"));
            this.ci.setGairport(rs.getString("gairport"));
            this.ci.setStime(rs.getString("stime"));
            this.ci.setGtime(rs.getString("gtime"));
            this.ci.setTicketprice(rs.getString("ticketprice"));
            return this.ci;
        } catch (SQLException ex) {
            this.ci = null;
            System.out.println("Method FlightsServiceI.findByPlaneid() failure!");
            ex.printStackTrace();
            return this.ci;
        }

    }

    public void insertFlights(FlightsI ci) {
        this.dbConnection();
        this.exeInsert("Insert Into flights(plane_id,start_airport,get_airport,start_time,get_time,ticket_price) Values ("+"'" + ci.getPlaneid() + "','" + ci.getSairport() + "','" + ci.getGairport() + "','" + ci.getStime() + "','" + ci.getGtime() + "','" + ci.getTicketprice()+ "')");
    }

    public void dbConnection() {
        try {
            conn.Load_DB();
            conn.Connection_DB();
        } catch (ClassNotFoundException ex) {
            System.out.println("Class Not Found!");
            ex.printStackTrace();
        } catch (SQLException sqle) {
            System.out.println("Connection Dababase failure!");
            sqle.printStackTrace();
        }
    }

    public void getResultSet(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    rs = st.executeQuery(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method FlightsServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public void exeInsert(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method FlightsServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public List findAll() {
        FlightsI cu;
        ArrayList list = new ArrayList();
        this.dbConnection();
        this.getResultSet("SELECT * FROM flights");
        try {
            while (rs.next()) {
                cu = new FlightsI();
                cu.setPlaneid(rs.getString("plane_id"));
                cu.setSairport(rs.getString("start_airport"));
                cu.setGairport(rs.getString("get_airport"));
                cu.setStime(rs.getString("start_time"));
                cu.setGtime(rs.getString("get_time"));
                cu.setTicketprice(rs.getString("ticket_price"));
                list.add(cu);
            }
        } catch (SQLException ex) {
            System.out.println("Method CustomerServiceI.findAll() failure!");
            ex.printStackTrace();
        }
        return list;
    }

    public void deleteFlights(FlightsI ci) {
        this.dbConnection();
        this.exeDelete("Delete From flights Where planeid='" + ci.getPlaneid()+"'");
    }

    public void exeDelete(String sql){
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method FlightsServiceI.exeDelete() failure!");
            sqle.printStackTrace();
        }
    }
}
